#ifndef RAYTRACER_H
#define RAYTRACER_H

#include <QThread>
#include <QColor>

class QImage;
class IScene;


class Raytracer : public QThread
{
    Q_OBJECT
    private:

    signals:

        void updateProgess(int percentageCompleted);
        void renderingFinished(QImage *image);

    protected:

        float normalize(float vec[]);
        void setViewParameters(float fovyDegree, float near);
        QColor traceRayAndGetColor(float rayEx, float rayEy, float rayEz, float rayVx, float rayVy, float rayVz);
        float calculateN(float fn[], float p1[], float p2[], float p3[]);

        //bounding box
        bool mashTest(float rayEx, float rayEy, float rayEz, float rayVx, float rayVy, float rayVz, float min[], float max[]);

        bool triangleTest(float p[], float p1[], float p2[], float p3[], float a, float ai[]);

        float fovyDegree;
        float near;
        float fovyRadians;

        float w, h;
        float aspect;

        float rayEx, rayEy, rayEz;    // eye point==ray starting point

    public:

        Raytracer(int width, int height, QObject *parent = 0);
        ~Raytracer();

        void startRendering(IScene*);

        int width;
        int height;
        QImage *image;

        virtual void run();
};

#endif // RAYTRACER_H
